Ext.define('App.management.pm.view.ProjectSelectionWindow', {
    extend: 'Ext.window.Window',
    alias: ['widget.ProjectSelectionWindow'],
    width: 500,
    height: 250,
    modal: true,
    layout: 'column', defaults: {columnWidth: 1, margin: 5},
    buttons: [{
        text: '确定',
        handler: function () {
            this.up('window').selectionProject();
        }
    }, {
        text: '取消',
        handler: function () {
            this.up('window').close();
        }
    }],
    items: [{
        xtype: 'panel',
        html: '<span style="color:crimson;">注：如果您存在多个项目需要管理，请先选择需要管理的项目，“确定”后在项目管理页面所有的操作将会针对此项目！</span>',
    }, {
        fieldLabel: '选择项目',
        xtype: 'combobox',
        labelAlign: 'top',
        editable: false,
        queryMode: 'local',
        valueField: 'id',
        displayField: 'name',
        store: {
            autoLoad: true,
            fields: ['id', 'name'],
            pageSize: 9999,
            remoteSort: true,
            sorters: [{
                property: 'name',
                direction: 'asc'
            }],
            proxy: {
                type: 'majax',
                url: 'project/query_simple',
                reader: {
                    type: 'json',
                    rootProperty: 'data',
                    totalProperty: 'total'
                }
            },
            listeners: {
                'beforeload': function (store, operation, eOpts) {
                    var params = {};
                    var manager = false;
                    for (var i = 0; i < app.user.authorities.length; i++) {
                        var authority = app.user.authorities[i].authority;
                        if (authority == 'ROLE_系统管理员' || authority == 'ROLE_项目管理员') {
                            manager = true;
                            break;
                        }
                    }
                    if (manager == false) {
                        params['params.pmNo_eq'] = window.app.user.chinasoftNumber
                    }
                    Ext.apply(operation._proxy.extraParams, params);
                }
            }
        }
    }],
    listeners: {
        afterrender: function () {
            if (!Ext.isEmpty(app.project)) {
                this.down('combobox').setValue(app.project.id)
            }
        }
    },
    selectionProject: function () {
        var me = this;
        var id = this.down('combobox').getValue();
        me.mask('切换...');
        Ext.Ajax.request({
            url: 'management/authorization/user/set_user_current_project',
            params: {id: id},
            success: function (resp) {
                if (resp.result.success == true) {
                    Ext.Msg.alert('提示', '您当前管理项目已经切换，系统将重新加载数据。', function () {
                        window.location.reload()
                    });
                } else {
                    me.unmask();
                }
            },
            callback: function () {
                // me.unmask();
            }
        });
    }
})
